package com.taxi.driver.module.order.pay;

import com.qianxx.network.RequestError;
import com.qianxx.utils.RxUtil;
import com.socks.library.KLog;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.config.ConfigRepository;
import com.taxi.driver.data.order.OrderRepository;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.event.OrderEvent;
import com.taxi.driver.event.PayEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class OrderPayPresenter extends BasePresenter implements OrderPayContract.Presenter {

    private OrderPayContract.View mView;
    private OrderRepository mOrderRepository;
    private UserRepository mUserRepository;
    private ConfigRepository mConfigRepository;

    private String mOrderUuid; //订单编号

    @Inject
    public OrderPayPresenter(OrderPayContract.View view, OrderRepository orderRepository, UserRepository userRepository, ConfigRepository configRepository) {
        mView = view;
        mOrderRepository = orderRepository;
        mUserRepository = userRepository;
        mConfigRepository = configRepository;
    }

    @Override
    public void onCreate() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPayEvent(PayEvent event) {
        switch (event.type) {
            case PayEvent.PAY_SUCCESS: //支付成功
                mView.paySuccess();
                payCallback();
                break;
            case PayEvent.PAY_FAIL: //支付失败
                mView.payFail();
                break;
            case PayEvent.PAY_REMOVE://清除支付缓存
                removePayCache();
                break;
        }
    }

    private void payCallback() {
        mConfigRepository.payCallback(mOrderUuid)
                .compose(RxUtil.applySchedulers())
                .subscribe(KLog::e,
                        KLog::e);
    }

    private void removePayCache() {
        mConfigRepository.removePayCache(mOrderUuid)
                .compose(RxUtil.applySchedulers())
                .subscribe(KLog::e,
                        KLog::e);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOrderEvent(OrderEvent event) {
        switch (event.type) {
            case OrderEvent.ORDER_PASSENGER_ORDER_PAYED: //支付已被用户订单
                mView.close();
                break;
            case OrderEvent.MODIFY_MONEY:
                double total = (double) event.obj1;
                mView.showPriceInfo(total);
                break;
        }
    }

    @Override
    public void setOrderUuid(String orderUuid) {
        mOrderUuid = orderUuid;
    }

    @Override
    public String getOrderUuid() {
        return mOrderUuid;
    }

    @Override
    public void reqPayByWeixin(String ip) {
        mSubscriptions.add(
                mOrderRepository.payByWechat(mOrderUuid, ip)
                        .compose(RxUtil.applySchedulers())
                        .doOnSubscribe(() -> mView.showLoadingView(true))
                        .doAfterTerminate(() -> mView.hideLoadingView())
                        .subscribe(entity -> {
                            mView.startWxpay(entity); //执行"微信支付"
                        }, ex -> {
                            if (ex instanceof RequestError) {
                                RequestError error = (RequestError) ex;
                                if (error.getErrCode() == 30002) {
                                    mView.paySuccess();
                                    removePayCache();
                                } else if (error.getErrCode() == 9000) {
                                    mView.toast(error.getMessage());
                                } else if (error.getErrCode() == 9001) {
                                    mView.paySuccess();
                                    removePayCache();
                                } else {
                                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                                    removePayCache();
                                }
                            }
                        }));
    }

    @Override
    public void reqPayByAlipay() {
        mSubscriptions.add(
                mOrderRepository.payByAlipay(mOrderUuid)
                        .compose(RxUtil.applySchedulers())
                        .doOnSubscribe(() -> mView.showLoadingView(true))
                        .doAfterTerminate(() -> mView.hideLoadingView())
                        .subscribe(s -> {
                            mView.startAlipay(s); //执行"支付宝支付"
                        }, ex -> {
                            if (ex instanceof RequestError) {
                                RequestError error = (RequestError) ex;
                                if (error.getErrCode() == 30002) {
                                    mView.paySuccess();
                                    removePayCache();
                                } else if (error.getErrCode() == 9000) {
                                    mView.toast(error.getMessage());
                                } else if (error.getErrCode() == 9001) {
                                    mView.paySuccess();
                                    removePayCache();
                                } else {
                                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                                    removePayCache();
                                }
                            }
                        }));
    }

    @Override
    public void reqPayByBalance() {
        mSubscriptions.add(
                mOrderRepository.payByBalance(mOrderUuid)
                        .compose(RxUtil.applySchedulers())
                        .doOnSubscribe(() -> mView.showLoadingView(true))
                        .doAfterTerminate(() -> mView.hideLoadingView())
                        .subscribe(s -> {
                            mView.toast("余额支付成功");
                            mView.paySuccess();
                        }, ex -> {
                            if (ex instanceof RequestError) {
                                RequestError error = (RequestError) ex;
                                if (error.getErrCode() == 30002) {
                                    mView.paySuccess();
                                    removePayCache();
                                } else if (error.getErrCode() == 9000) {
                                    mView.toast(error.getMessage());
                                } else if (error.getErrCode() == 9001) {
                                    mView.paySuccess();
                                    removePayCache();
                                } else {
                                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                                    removePayCache();
                                }
                            }
                        }));
    }

    @Override
    public int driverType() {
        return mUserRepository.getDriverType();
    }

}
